
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import junit.framework.TestCase;


public class FileContentAutoTagAlgorithmTest extends TestCase {
	public void testGetTagsForFile() {
		FileContentAutoTagAlgorithm pathAutoTagAlgorithm=new FileContentAutoTagAlgorithm();
		
		
		//create new file to contain mapping info.
		Path mappingfile=Paths.get("mappingfile.txt");
		
		//create the file.
		//
		try {
			mappingfile.createFile();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		
		//write the mapping data into it.
		//
		BufferedWriter out=null;
		try {
			out = new BufferedWriter(new FileWriter(mappingfile.toString())); 
			
			//default values.
			String[] data={"operation systems:windows,linux,solaris" ,
						   "studies:grade,lecture,exercise" 		 ,
						   "family:dad,mom,brother,sister"
				 		  };
			int size=data.length;
			for(int i=0 ; i<size ; i++){
				out.write(data[i]);
				out.newLine();
			}
		} 
		catch (IOException x) {
		}
		//close the file
		finally {
		    if (out != null)
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		//create the test file.
		Path testFile=Paths.get("testFile.txt");
		//create the file.
		//
		try {
			testFile.createFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		//write data into test file.
		//
		try {
			out = new BufferedWriter(new FileWriter(testFile.toString())); 
			
			//default values.
			String[] data={"me,mom and dad use linux so much.but my !@@#31! brother like windows." };
			int size=data.length;
			for(int i=0 ; i<size ; i++){
				out.write(data[i]);
				out.newLine();
			}
		} 
		catch (IOException x) {
		}
		//close the file
		finally {
		    if (out != null)
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		
		//sets algorithm mapping data with the file  we created.
		pathAutoTagAlgorithm.buildFromFile(mappingfile);
	    	
		// get the tags ussing the algo' from checking the test file.
		String[] algoResult=pathAutoTagAlgorithm.getTagsForFile(new File(testFile.toUri()));
		//set the calculated result.
		String[] calculatedResult={"family","operation systems"};
		//delete the files.
		try {
			mappingfile.delete();
			testFile.delete();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(algoResult.length!=calculatedResult.length){
			fail();
		}
		else{
			for(int i=0;i<algoResult.length;i++){
				if(!algoResult[i].equals(calculatedResult[i]))
					fail();
			}
		}
		
		
	}
}
